<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="出库单号" prop="outboundNo">
        <el-input v-model="queryParams.outboundNo" placeholder="请输入采购单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="queryParams.customerName" placeholder="请输入供应商名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="procureTime">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['cld:statistics:export']">列表导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" row-key="outboundDetailsId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true"/>
      <el-table-column label="出库单号" align="center" prop="outboundNo" sortable/>
      <el-table-column label="销售单号" align="center" prop="saleOdd" sortable/>
      <el-table-column label="客户编号" align="center" prop="customerNo" sortable/>
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="出库日期" align="center" prop="outboundTime" sortable width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.outboundTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="仓库名称" align="center" prop="wareName" sortable width="120"/>
      <el-table-column label="出库数量" align="center" prop="realityNumber" sortable width="120"/>
      <el-table-column label="物流费" align="center" prop="logisticsCost" sortable width="120">
        <template slot-scope="scope">
          <span>{{ formattedNumber(scope.row.logisticsCost) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="销售人" align="center" prop="salePersonName" width="120"/>
      <el-table-column label="录单人" align="center" prop="createName" width="120"/>
      <el-table-column label="出库状态" align="center" prop="outboundStatus" sortable width="120">
        <template slot-scope="scope">
          <el-tag :type="scope.row.outboundStatus == 1 ? 'danger' : 'success'">{{ scope.row.outboundStatus == 1 ? '未出库' : '已出库' }}</el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { outboundStatistics } from "@/api/erp/statistics";
import {formattedNumber} from "@/utils/ruoyi";

export default {
  name: "ErpOutbound",
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      total: 0,
      // 多选ids
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 出库统计表格数据
      list: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        outboundNo: null,
        customerName: null,
        outboundStatus: null,
      },
      // 出库状态
      options: [
        {
          label: '未出库',
          value: 1
        }, {
          label: '已出库',
          value: 2
        },
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    formattedNumber,
    /** 查询采购单列表 */
    getList() {
      this.loading = true;
      outboundStatistics(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.outboundDetailsId)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "erp/statistics/exportOutbound",
        {
          ...this.queryParams,
          exportIds: this.ids?.join(',')
        },
        `出库统计_${new Date().getTime()}.xlsx`
      );
    },
  }
};
</script>
<style scoped>
.el-range-editor.el-input__inner {
  width: 100% !important;
}
</style>
